﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.ComponentModel;
using System.Reflection;

namespace ProjectNocturnalFool
{

    public sealed class XMLMgr
    {
        /// <summary>
        /// Allocate ourselves.
        /// We have a private constructor, so no one else can.
        /// </summary>
        static readonly XMLMgr _instance = new XMLMgr();

        /// <summary>
        /// Access SiteStructure.Instance to get the singleton object.
        /// Then call methods on that instance.
        /// </summary>
        public static XMLMgr Instance
        {
            get { return _instance; }
        }


        public static bool editAbilityXML()
        {
            //var fileStream = new FileStream("Content/AbilityDefinitions.xml", FileMode.Open);
            //List<AbilityDefinition> abilityList = new List<AbilityDefinition>();

            //var xmlSerializer = new XmlSerializer(typeof(List<AbilityDefinition>));

            //AbilityDefinition ability = new AbilityDefinition();
            //ability.name = "FireBlast";
            //ability.attributes.Add(AbilityAttribute.Fire);
            //ability.description = "Short Range Fire Blast";
            //ability.range = 3;
            //ability.AOE = AreaOfEffect.Single;
            //ability.damage = 3;
            //ability.cooldown = 4;
            //abilityList.Add(ability);

            //ability = new AbilityDefinition();
            //ability.name = "Meteo";
            //ability.attributes.Add(AbilityAttribute.Fire);
            //ability.attributes.Add(AbilityAttribute.Arcane);
            //ability.description = "Long Range Meteor Attack";
            //ability.range = 8;
            //ability.AOE = AreaOfEffect.Blast;
            //ability.damage =8;
            //ability.cooldown = 8;
            //abilityList.Add(ability);

            //xmlSerializer.Serialize(fileStream, abilityList);
    
               return true;
        }



        public static List<AbilityDefinition> getAbilityList(AbilityAttribute attribute)
        {
            XDocument doc = XDocument.Load(typeof(XMLMgr).Assembly.GetManifestResourceStream("ProjectNocturnalFool.AbilityDefinitions.xml"));
                           
            List<AbilityDefinition> abilityList = new List<AbilityDefinition>();
            

            abilityList = (from ability in doc.Descendants("AbilityDefinition")
                           where ability.Descendants("attributes").Elements().Any(p => (string)p.Value == attribute.Description())
                           select new AbilityDefinition()
                           {
                               name = Convert.ToString(ability.Element("name").Value),
                               description = Convert.ToString(ability.Element("description").Value),
                               range = Convert.ToInt32(ability.Element("range").Value),
                               AOE = (AreaOfEffect)GetEnumFromDescription(Convert.ToString(ability.Element("AOE").Value), typeof(AreaOfEffect) ),
                               cooldown = Convert.ToInt32(ability.Element("cooldown").Value),
                               damage = Convert.ToInt32(ability.Element("cooldown").Value),
                               attributes = ability.Descendants("attributes").Elements().Select(p => (AbilityAttribute)GetEnumFromDescription((string)p.Value, typeof(AbilityAttribute))).ToList(),
                           }).ToList();

            return abilityList;
        }

        public static int GetEnumFromDescription(string description, Type enumType)
        {
             
            var field = enumType.GetField(description);
            if (field != null)
            {
                var attributes = field.GetCustomAttributes(typeof(DescriptionAttribute),
                                                           false);
                foreach (DescriptionAttribute currentAttribute in attributes)
                {
                    foreach (Enum val in Enum.GetValues(enumType))
                    {
                        if (val.Description() == currentAttribute.Description)
                        {
                            return (int)Enum.Parse(enumType, val.ToString());
                        }
                    }
                }                
            }
            return 0;
        }
    }

}
